angular.module('mamApp').controller('CPSelectModalController', ['$scope', 'param', '$uibModalInstance', 'CommonManager', '$filter', function ($scope, param, $uibModalInstance, CommonManager, $filter) {
    $scope.total = 0;
    $scope.table = JSON.parse(localStorage.getItem("codeTable"))
    $scope.levels=param.Lvs;
    $scope.signs=param.signs;
    $scope.$parent.allCheckedCP = false;
    $scope.tmpSelect = [];
    $scope.cpTypes = [
        {key: 41, value: $scope.table[41]},
        {key: 51, value: $scope.table[51]},
        {key: 52, value: $scope.table[52]},
        {key: 53, value: $scope.table[53]},
        {key: 54, value: $scope.table[54]}
    ];
    $scope.fileNames = [{key: "", value: "查找字段"},
        {key: "cpId", value: "CP_ID"},
        {key: "nickName",value: "昵称"},
        {key: "userName", value: "账户名"}];


    $scope.search={key:""};
    var getCpList = function (pageNo, pageSize,query) {
        CommonManager.cpList([pageNo, pageSize],query).then(function (result) {
            $scope.items = result.data.pageList;
            $scope.paginationConf.totalItems = result.data.total;

        })
    }


    $scope.searchClick=function (event) {
        if(event&&event.key=="Enter"||!event){
            getCpList(1,5,$scope.search);
        }
    }




    $scope.selectChange=function () {
        getCpList(1,5,$scope.search);
    }


    $scope.allCheckChange = function (allChecked) {
        if (allChecked) {
            $scope.items.forEach(function (item) {
                item.checked = allChecked;
                //如果不存在则push，否则啥都不干
                $filter('filter')($scope.tmpSelect, {cpId: item.cpId}).length ? null : $scope.tmpSelect.push(item);
            })
        } else {
            $scope.items.forEach(function (item) {
                item.checked = allChecked;
                $scope.tmpSelect.forEach(function (select) {
                    if (select.cpId == item.cpId) {
                        $scope.tmpSelect.splice($scope.tmpSelect.indexOf(select), 1);
                    }
                })
            })
        }

    }
    $scope.itemCheckChange = function (item) {
        if (item.checked) {
            $scope.tmpSelect.push(item);
        } else {
            //取消勾选则从选中的数组中排除
            $scope.tmpSelect.forEach(function (select) {
                if (select.cpId == item.cpId) {
                    $scope.tmpSelect.splice($scope.tmpSelect.indexOf(select), 1);
                }
            })
        }

        $scope.$parent.allCheckedCP = true;
        $scope.items.forEach(function (item) {
            if (!item.checked) {
                $scope.$parent.allCheckedCP = false;

            }
        })
    }
    $scope.modelOk = function ($event, item) {
        $uibModalInstance.close($scope.tmpSelect);
    };

    $scope.modelCancel = function () {
        $uibModalInstance.dismiss("cancel");
    };


    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 5,
        pagesLength: 9,
        perPageOptions: [10, 20, 30, 40, 50],
        search: true,
        onChange: function () {
            getCpList(this.currentPage, this.itemsPerPage,$scope.search);
        }
    };
}])

